<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html><head><title>Programming in Lua : 16.5</title>
<link rel="stylesheet" type="text/css" href="pil.css">
</head>
<body>
<p class="warning">
<A HREF="contents.html"><IMG TITLE="Programming in Lua (first edition)" SRC="capa.jpg" ALT="" ALIGN="left"></A>This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.<BR>The third edition targets Lua 5.2 and is available at <A HREF="http://www.amazon.com/exec/obidos/ASIN/859037985X/theprogrammil3-20">Amazon</A> and other bookstores.<BR>By buying the book, you also help to <A HREF="../donations.html">support the Lua project</A>.
</p>
<table width="100%" class="nav">
<tr>
<td width="10%" align="left"><a href="16.4.html"><img border="0" src="left.png" alt="Previous"></a></td>
<td width="80%" align="center"><a href="contents.html"><font face="Helvetica,Arial,sanserif">
<font color="gray">Programming in </font><font color="blue"> Lua</font>
</font></a></td>
<td width="10%" align="right"><a href="17.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
<tr>
<td width="10%" align="left"></td>
<td width="80%" align="center"><a href="contents.html#P2">Part II. Tables and Objects</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="contents.html#16">Chapter 16. Object-Oriented Programming</a></td>
<td width="10%" align="right"></td></tr>
</table>
<hr/>
<p><h2>16.5 &ndash; The Single-Method Approach</h2>

<p>A particular case of the previous approach for OO programming
occurs when an object has a single method.
In such cases, we do not need to create an interface table;
instead, we can return this single method as the object representation.
If this sounds a little weird,
it is worth remembering <a href="7.1.html#iter-closure">Section 7.1</a>,
where we saw how to construct iterator functions
that keep state as closures.
An iterator that keeps state is nothing more
than a single-method object.

<p>Another interesting case of single-method objects occurs when
this single-method is actually a dispatch method that
performs different tasks based on a distinguished argument.
A possible implementation for such object is as follows:
<pre>
    function newObject (value)
      return function (action, v)
        if action == "get" then return value
        elseif action == "set" then value = v
        else error("invalid action")
        end
      end
    end
</pre>
Its use is straightforward:
<pre>
    d = newObject(0)
    print(d("get"))    --> 0
    d("set", 10)
    print(d("get"))    --> 10
</pre>

<p>This unconventional implementation for objects is quite effective.
The syntax <code>d("set",10)</code>,
although peculiar,
is only two characters longer than
the more conventional <code>d:set(10)</code>.
Each object uses one single closure,
which is cheaper than one table.
There is no inheritance, but we have full privacy:
The only way to access an object state is through its sole method.

<p>Tcl/Tk uses a similar approach for its widgets.
The name of a widget in Tk denotes a function
(a <em>widget command</em>) that can perform all kinds of
operations over the widget.

<p>
<hr/>
<table width="100%" class="nav">
<tr valign="top">
<td width="90%" align="left">
<small>
  Copyright &copy; 2003&ndash;2004 Roberto Ierusalimschy.  All rights reserved.
</small>
</td>
<td width="10%" align="right"><a href="17.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
</table>


</body></html>

